//
//  EmoticonViewModel.swift
//  01-表情键盘(绑定数据)
//
//  Created by gxiangzi on 15/9/13.
//  Copyright © 2015年 hqu. All rights reserved.
//

import UIKit

class EmoticonViewModel: NSObject {
    
    lazy var packages = [EmoticonPackage]()
    
    func loadEmoticon() {
        
        // 添加一个最近的
        packages.append(EmoticonPackage(dict: ["group_name_cn":"最近"]))
        
        /// 获得路径
        let  path = NSBundle.mainBundle().pathForResource("emoticons", ofType: "plist", inDirectory: "Emoticons.bundle")
        
        /// 获得对应的字典 & 字典中的 packages 中的数据
        let dict = NSDictionary(contentsOfFile: path!)
        
        let array = dict!["packages"] as! [[String : AnyObject]]
        
        for infoDict in array {
            let id = infoDict["id"] as! String
            
            // 获得路径
            let emoPath = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle" + "/" + id)
            // 3> 加载 info.plist 字典
            let packageDict = NSDictionary(contentsOfFile: emoPath!) as! [String: AnyObject]
            
            // 4> 字典转模型
            packages.append(EmoticonPackage(dict: packageDict))
        }
    }
    
    /// 定义一个方法，根据indexPath 返回相应的数组
    func emoticon(indexPath:NSIndexPath) -> Emoticon {
        return packages[indexPath.section].emoticons[indexPath.item]
    }
    
}

